﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HL.Lib.Models;
using HL.Core.Models;
using HL.Lib.Global.ListItem;
using System.IO;
namespace HL.Lib.Global
{
    public class MyClass
    {
        public static SysPageEntity getParentPage(int parentID)
        {
            SysPageEntity entity = SysPageService.Instance.CreateQuery()
                .Where(o => o.ID == parentID)
                .ToSingle();
            if (entity.ParentID != 0) return entity; else return null;
        }
        //Lấy dung lượng File kb
        public static long getFileSize(string path)
        {
            long dl = 0;
            FileInfo file = new FileInfo(path);
            if (file.Exists)
            {
                dl = file.Length;
            }
            return dl/1024;
        }        
        //
        //
        public static string GetCusTomPage(string keyCusTom, string custom)
        {
            string[] array = null;
            string design = string.Empty;
            array = custom.Split('\n');
            if (array != null)
                for (int u = 0; u < array.Length; u++)
                {
                    string s = array[u].Trim();
                    if (string.IsNullOrEmpty(s)) continue;
                    if (s.StartsWith("\\")) continue;
                    int index = s.IndexOf("=");
                    if (index == -1) continue;
                    string key = s.Substring(0, index).Trim();
                    string value = s.Substring(index + 1).Trim();
                    if (key == keyCusTom) design = value;
                }
            return design;
        }
        public static string[] GetColor(string custom)
        {
            string[] array = null;
            array = custom.Trim().Split(',');
            return array;
        }
        public static string RePlaceCustom(string keyCusTom, string custom,string customid)
        {
            string[] array = null;
            string newcustom = string.Empty;
            array = custom.Split('\n');
            if (array != null)
                for (int u = 0; u < array.Length; u++)
                {
                    string s = array[u].Trim();
                    if (string.IsNullOrEmpty(s)) continue;
                    if (s.StartsWith("\\")) continue;
                    int index = s.IndexOf("=");
                    if (index == -1) continue;
                    string key = s.Substring(0, index).Trim();
                    string value = s.Substring(index + 1).Trim();
                    if (key == keyCusTom) newcustom = custom.Replace(key=value,customid);
                }
            return newcustom;
        }
        //ghi file
        public static void WriteFileFS(string path, string content,FileMode fd)
        {
            FileStream fs = new FileStream(path, fd , FileAccess.Write);
            StreamWriter sw = new StreamWriter(fs);
            sw.Write(content);
            sw.Close();
            fs.Close();
        }
        //setting key
            public static string ShowCheckBoxBySettingkey(string settingKey, string name, int State)
            {
                List<Item> list = List.GetListByText(settingKey);

                string s = string.Empty;

                for (int i = 0; list != null && i < list.Count; i++)
                {
                    s += "<input name=\"" + name + "\"" + ((State & HL.Core.Global.Convert.ToInt(list[i].Value)) ==

                    HL.Core.Global.Convert.ToInt(list[i].Value) ? "checked=\"checked\"" : string.Empty) + " value=\"" + list

                    [i].Value + "\" type=\"checkbox\" />" + list[i].Name + " &nbsp;";
                }
                return s;
           }
            public static string ShowOptionBySettingkey(string settingKey, string name, int State)
            {
                List<Item> list = List.GetListByText(settingKey);

                string s = string.Empty;

                for (int i = 0; list != null && i < list.Count; i++)
                {
                    s += "<input name=\"" + name + "\"" + ((State & HL.Core.Global.Convert.ToInt(list[i].Value)) ==

                    HL.Core.Global.Convert.ToInt(list[i].Value) ? "checked=\"checked\"" : string.Empty) + " value=\"" + list

                    [i].Value + "\" type=\"radio\" />" + list[i].Name + " &nbsp;";
                }
                return s;
            }
            public static string ShowDLLBySettingkey(string settingKey,int State)
            {
                List<Item> list = List.GetListByText(settingKey);

                string s = string.Empty;

                for (int i = 0; list != null && i < list.Count; i++)
                {
                    s += "<option " + ((State & HL.Core.Global.Convert.ToInt(list[i].Value)) ==

                    HL.Core.Global.Convert.ToInt(list[i].Value) ? "selected=\"selected\"" : string.Empty) + " value=\"" + list

                    [i].Value + "\" >" + list[i].Name + "</option>";
                }
                return s;
            }
        public static List<Item> GetListByText(string ListText)
        {
            List<Item> list = new List<Item>();

            string[] _Items = ListText.Trim().Split(',');
            for (int i = 0; i < _Items.Length; i++)
            {
                if (_Items[i].IndexOf('|') == -1)
                    list.Add(new Item(_Items[i], _Items[i]));
                else
                {
                    string _Name = _Items[i].Split('|')[0];
                    string _Value = _Items[i].Split('|')[1];

                    list.Add(new Item(_Name, _Value));
                }
            }

            return list;
        }
        public static int GetState(int[] arrState)
        {
            int state = 0;

            for (int i = 0; arrState != null && i < arrState.Length; i++)
                if (arrState[i] > 0) state ^= arrState[i];

            return state;
        }
    }
}
